
ALTER PROCEDURE [dbo].[MovePlayer] 
	@Game_ID numeric(10,0) = null,
	@Player_ID numeric(10,0) = null
AS
BEGIN 
	DECLARE @dice1 int
	DECLARE @dice2 int
	DECLARE @maxPosition int
	DECLARE @newPosition int

	CREATE TABLE #tmpTable
	(
		dice1 int,
		dice2 int
	)

	INSERT INTO #tmpTable (dice1,dice2)
	EXEC [dbo].[RollDice]
	
	SELECT
		@dice1 = #tmpTable.dice1,@dice2 = #tmpTable.dice2
	FROM 
		#tmpTable
		
	DROP TABLE #tmpTable
	
	--print @dice1
	--print @dice2
	
	SET @maxPosition = (SELECT MAX(Position) as MaximumPosition FROM [dbo].[Land] where IsActive = 'Y')
	SET @newPosition = (SELECT Position+@dice1+@dice2
						FROM [dbo].[Player] p
							join [dbo].[PlayerEnrollment] pe on (p.Player_ID = pe.player_id and p.[Type] in ('R') )
							where pe.IsActive = 'Y'
							and pe.Game_ID = @Game_ID
							and pe.Player_ID = @Player_ID)
	
	SET @newPosition = @newPosition%@maxPosition
	IF @newPosition = 0
		BEGIN
			SET @newPosition = 1
		END
	
	Update [dbo].[PlayerEnrollment]
	SET Position = @newPosition,IsMyMove = 'N'
	WHERE IsActive = 'Y'
		and Game_ID = @Game_ID
		and Player_ID = @Player_ID
		
    SELECT pe.Player_ID, pe.Position
	FROM [dbo].[Player] p
	join [dbo].[PlayerEnrollment] pe on (p.Player_ID = pe.player_id and p.[Type] in ('R') )
	where pe.IsActive = 'Y'
	and pe.Game_ID = @Game_ID
	and pe.Player_ID = @Player_ID
	
END
 
-- [dbo].[MovePlayer]  1,3